package dao;

import banco.ConnectionDB;
import java.awt.Frame;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.Venda;

public class VendaDAOImp implements GenericDAOImp<Venda> {

    private Frame frame;
    private Connection connection;

    public VendaDAOImp(Frame frame) {
        this.frame = frame;

        connection = new ConnectionDB().getConnection();
    }

    @Override
    public void adiciona(Venda venda) {
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "INSERT INTO venda (venda_ID, cliente, produto, data, valor) "
                    + "VALUES (?, ?, ?, ?, ?)");

            statement.setInt(1, venda.getVendaID());
            statement.setString(2, venda.getCliente());
            statement.setString(3, venda.getProduto());
            statement.setString(4, venda.getData());
            statement.setDouble(5, venda.getValor());

            statement.execute();
            statement.close();
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame, "Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public void altera(Venda venda) {
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "UPDATE venda SET cliente = ?, "
                    + "produto = ?, data = ?, "
                    + "valor = ? "
                    + "WHERE venda_ID = ?");

            statement.setString(1, venda.getCliente());
            statement.setString(2, venda.getProduto());
            statement.setString(3, venda.getData());
            statement.setDouble(4, venda.getValor());
            statement.setInt(5, venda.getVendaID());

            statement.execute();
            statement.close();
        } catch (SQLException sQLException) {
            System.out.println("Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public void remove(Object iD) {
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "DELETE FROM venda "
                    + "WHERE venda_ID = ?");

            statement.setObject(1, iD);

            statement.execute();
            statement.close();
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame, "Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public Venda listaPorID(Object iD) {
        try {
            Venda venda = null;
            PreparedStatement statement = connection.prepareStatement(
                    "SELECT cliente, produto, data, valor "
                    + "FROM venda "
                    + "WHERE venda_ID = ?");

            statement.setObject(1, iD);

            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {

                venda = new Venda();
                venda.setVendaID((Integer) iD);
                venda.setCliente(resultSet.getString(1));
                venda.setProduto(resultSet.getString(2));
                venda.setData(resultSet.getString(3));
                venda.setValor(resultSet.getDouble(4));
            }

            resultSet.close();
            statement.close();

            return venda;
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame, "Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public Venda consultaSeta(String iD, int seta) {
        try {
            Venda venda = null;
            String query = "SELECT venda_ID, cliente, produto, data, valor "
                    + "FROM venda ";
            PreparedStatement statement = null;

            switch (seta) {
                case 1:
                    statement = connection.prepareStatement(
                            query + "ORDER BY venda_ID;");

                    break;
                case 2:
                    statement = connection.prepareStatement(
                            query + "WHERE venda_ID < ? "
                            + "ORDER BY venda_ID DESC;");

                    statement.setString(1, iD);

                    break;
                case 3:
                    statement = connection.prepareStatement(
                            query + "WHERE venda_ID > ? "
                            + "ORDER BY venda_ID;");

                    statement.setString(1, iD);

                    break;
                case 4:
                    statement = connection.prepareStatement(
                            query + "ORDER BY venda_ID DESC;");

                    break;
            }

            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {

                venda = new Venda();

                venda.setVendaID(resultSet.getInt(1));
                venda.setCliente(resultSet.getString(2));
                venda.setProduto(resultSet.getString(3));
                venda.setData(resultSet.getString(4));
                venda.setValor(resultSet.getDouble(5));
            }

            resultSet.close();
            statement.close();

            return venda;
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame,
                    "Erro ao executar comando no banco de dados!\n"
                    + "O programa vai ser fechado.\n\nErro ocorrido:\n"
                    + sQLException.getMessage());

            System.exit(0);

            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public List<Venda> listaTudo() {
        try {
            List<Venda> vendas = new ArrayList<Venda>();

            PreparedStatement statement = connection.prepareStatement(
                    "SELECT cliente, produto, data, valor "
                    + "FROM venda "
                    + "ORDER BY venda_ID");

            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next()) {

                Venda venda = new Venda();

                venda.setCliente(resultSet.getString(1));
                venda.setProduto(resultSet.getString(2));
                venda.setData(resultSet.getString(3));
                venda.setValor(resultSet.getDouble(4));

                vendas.add(venda);
            }

            resultSet.close();
            statement.close();

            return vendas;
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame, "Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }

    @Override
    public int consultaProximoID() {
        try {
            PreparedStatement statement = connection.prepareStatement(
                    "SELECT * "
                    + "FROM venda "
                    + "ORDER BY venda_ID DESC");

            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                return resultSet.getInt(1) + 1;
            }

            resultSet.close();
            statement.close();

            return 1;
        } catch (SQLException sQLException) {
            JOptionPane.showMessageDialog(frame, "Erro ao executar comando no banco de dados!\n"
                    + sQLException.getMessage());
            throw new RuntimeException(sQLException);
        }
    }
}
